﻿using AuthenticationService.Utils;
using Microsoft.AspNetCore.Mvc;

namespace AuthenticationService.Controllers
{
    [Route("weixin")]
    [ApiController]
    public class WeController : ControllerBase
    {
        [HttpGet("callback")]
        public string Verify()
        {
            // 从HTTP请求的查询字符串中获取参数
            string sVerifyMsgSig = HttpUtils.ParseUrl(Request, "msg_signature");
            string sVerifyTimeStamp = HttpUtils.ParseUrl(Request, "timestamp");
            string sVerifyNonce = HttpUtils.ParseUrl(Request, "nonce");
            string sVerifyEchoStr = HttpUtils.ParseUrl(Request, "echostr");

            // 配置企业微信的Token、AES密钥和CorpID
            string sToken = "";  // 企业微信 Token
            string sEncodingAESKey = "";  // 企业微信 AES密钥
            string sCorpID = "";  // 企业微信 CorpID

            // 创建验证实例
            Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID);

            // 用于存放验证成功后的回调字符串
            string sEchoStr = "";

            // 调用企业微信的VerifyURL方法进行验证
            int ret = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr, ref sEchoStr);

            // 如果验证失败，返回错误信息
            if (ret != 0)
            {
                return "Error: VerifyURL failed, ret: " + ret;
            }

            return sEchoStr;
        }
    }
}